<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="close">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="selRoles">
      <el-checkbox v-for="role in list" :key="role.id" :label="role.id">{{ role.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="saverole">确定</el-button>
      <el-button size="small" @click="$emit('update:showRoleDialog')">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 获取角色列表
import { getRoleList } from '@/api/setting'
// 获取用户角色回显
import { getUserDetailById } from '@/api/user'
// 分配角色
import { assignRoles } from '@/api/employees'
export default {

  props: {
    showRoleDialog: {
      type: Boolean,

      default: false
    }
  },
  data () {
    return {
      // 角色列表
      list: [],
      // 多选数据
      selRoles: [],
      // 用户ID
      userId: ''
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    // 分配角色
    async saverole () {
      try {
        await assignRoles({ id: this.userId, roleIds: this.selRoles })
        this.$message.success('分配成功')
        this.$emit('update:showRoleDialog')
      } catch (error) {
        console.log(error)
      }

      // console.log(data)
    },
    // 回显
    async getUserDetailById (id) {
      const data = await getUserDetailById(id)
      console.log(data)
      this.userId = id
      this.selRoles = data.data.roleIds || []
    },
    // 角色列表
    async getRoleList () {
      const data = await getRoleList({ page: 1, pagesize: 1000 })
      console.log(data.data.rows)
      this.list = data.data.rows
    },
    close () {
      this.$emit('update:showRoleDialog')
    }
  }
}
</script>
